﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDSP.BRL;
using System.Data;

namespace LDSP.UI.Admin
{
    public partial class AddChemical : BRL.BaseSysAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    this.CheckQS();
                }
                if (this.RB())
                {
                    if (!IsPostBack)
                    {
                        this.InitialDDL();
                        this.InitDataSelect();
                    }
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        private void InitialDDL()
        {
            DataTable dt = new ChemicalCatalog().GetAll();
            this.ddl_ChemicalCatalog.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddl_ChemicalCatalog.Items.Add(new ListItem(dt.Rows[i]["name"].ToString(), dt.Rows[i]["id"].ToString()));
            }
        }

        protected Chemical m_chemical = null;

        protected int LastID
        {
            get { return this.GetVSInt("LastID"); }
            set
            {
                this.SetVS("LastID", value);
                m_chemical = null;
            }
        }
        protected Chemical Chemical
        {
            get
            {
                if (m_chemical == null)
                {
                    m_chemical = new Chemical();
                    if (LastID != 0)
                    {
                        m_chemical.GetProperties(LastID);
                    }
                }
                return m_chemical;
            }
        }
        protected bool IsAdd
        {
            get { return LastID == 0; }
        }

        protected void CheckQS()
        {
            LastID = this.GetInt(this.Request.QueryString["id"]);
            if (Chemical.ID == 0)
            {
                LastID = 0;
            }
        }
        protected bool RB()
        {
            return this.AllowSystemUser();
        }
        protected void SetStatus()
        {
            this.lbl_op.Text = IsAdd ? "【添加】" : "【修改】";
            this.lbl_op.Style["color"] = IsAdd ? "red" : "blue";
            this.ClientComfirm(this.btn_ok, string.Format("您确定执行{0}操作吗？", IsAdd ? "添加" : "修改"));
        }
        protected void InitDataSelect()
        {
            this.txt_name.Text = Chemical.Name;
            this.txt_alias.Text = Chemical.Alias;
            this.txt_cas.Text = Chemical.CAS;
            this.txt_unit.Text = Chemical.Unit;
            this.txt_dangerNumber.Text = Chemical.DangerNumber;
            this.txt_dangerType.Text = Chemical.DangerType;
            this.txt_goodsNumber.Text = Chemical.GoodsNumber;
            this.txt_limitedScope.Text = Chemical.LimitedScope;
            this.txt_un.Text = Chemical.UN;
            this.txt_catalogorder.Text = Chemical.CatalogOrder;
            this.txt_PackageType.Text = Chemical.PackageType;
            this.txt_chemicalType.Text = Chemical.ChemicalType;
            this.txt_transportMethod.Text = Chemical.TransportMethod;
            this.txt_englishName.Text = Chemical.EnglishName;
            this.txt_typeCatalog.Text = Chemical.TypeCatalog;
            this.txt_secondaryDangerous.Text = Chemical.SecondaryDangerous;
            this.txt_limitationNumber.Text = Chemical.LimitationNumber;
            this.txt_exceptionNumber.Text = Chemical.ExceptionNumber;

            //化学品名录
            try
            {
                this.ddl_ChemicalCatalog.SelectedIndex = 0;
                this.ddl_ChemicalCatalog.SelectedValue = Chemical.Catalog;
            }
            catch { }

            this.SetStatus();
        }

        protected void ValidateDataBefore()
        {
        }
        protected void ValidateDataAfter()
        {
            if (Chemical.Name == "")
            {
                throw new BYFW.BOL.CustomException("名称不能为空！");
            }
        }
        protected void SetData()
        {
            this.ValidateDataBefore();
            Chemical.Name = this.txt_name.Text.Trim();
            Chemical.Alias = this.txt_alias.Text;
            Chemical.CAS = this.txt_cas.Text;
            Chemical.Catalog = this.ddl_ChemicalCatalog.SelectedValue;
            Chemical.Unit = this.txt_unit.Text;
            Chemical.DangerNumber = this.txt_dangerNumber.Text;
            Chemical.DangerType = this.txt_dangerType.Text;
            Chemical.GoodsNumber = this.txt_goodsNumber.Text;
            Chemical.LimitedScope = this.txt_limitedScope.Text;
            Chemical.UN = this.txt_un.Text;
            Chemical.CatalogOrder = this.txt_catalogorder.Text;
            Chemical.PackageType = this.txt_PackageType.Text;
            Chemical.ChemicalType = this.txt_chemicalType.Text;
            Chemical.TransportMethod = this.txt_transportMethod.Text;
            Chemical.EnglishName = this.txt_englishName.Text;
            Chemical.TypeCatalog = this.txt_typeCatalog.Text;
            Chemical.SecondaryDangerous = this.txt_secondaryDangerous.Text;
            Chemical.LimitationNumber = this.txt_limitationNumber.Text;
            Chemical.ExceptionNumber = this.txt_exceptionNumber.Text;

            this.ValidateDataAfter();
        }
        protected void Add()
        {
            this.SetData();
            Chemical.Add();
            this.LastID = 0;
            this.JSAlert("添加成功！");
        }
        protected void Update()
        {
            this.SetData();
            Chemical.Update();
            this.JSAlert("更新成功！");
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            try
            {
                this.LastID = 0;
                this.InitDataSelect();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_ok_Click(object sender, EventArgs e)
        {
            try
            {
                if (IsAdd)
                {
                    this.Add();
                }
                else
                {
                    this.Update();
                }
                this.InitDataSelect();

            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
    }

}